% computes ordinary coherence
% between two channel
% input data in EDI spectra pack
% latest date 21.03.2003
% Reference : Bendat, J.S. and Piersol, A.G. 1971. Random data: Analysis and measurement procedures\
% John Wiley & Sons, NY. Pages 141-147
% tested with Geotools - correct 24.03.2003

function[c] = ocoh(SPM,x,y);


if isstruct(SPM) == 1,
   for i = 1:length(SPM.spectra),
      SPM1(i,:,:)=SPM.spectra(i).data;
   end;
   clear SPM;
   SPM=SPM1;
end;
   

A = size(SPM);
k = length(A);
j = sqrt(-1);

if k == 2,
data = SPM;
ab = data(x,y)+j*data(y,x);
aa = data(x,x);
bb = data(y,y);
c = abs(ab)^2/(aa*bb);
elseif k == 3,
   for i = 1:A(1),
      data(:,:) = SPM(i,:,:);
		ab = data(x,y)+j*data(y,x);
		aa = data(x,x);
		bb = data(y,y);
      c(i) = abs(ab)^2/(aa*bb);
   end;
elseif k == 4,
   for i = 1:A(2),
      for ii = 1:A(1),
      data(:,:) = SPM(ii,i,:,:);
		ab = data(x,y)+j*data(y,x);
		aa = data(x,x);
		bb = data(y,y);
      c(ii,i) = abs(ab)^2/(aa*bb);
      end;
   end;
else,
   c = 999*999;
end;

c = sqrt(c);